package org.jeecg.modules.veterans.service.impl;

import org.jeecg.modules.system.entity.SysVeteranSta;
import org.jeecg.modules.system.vo.SysNameValueVo;
import org.jeecg.modules.veterans.entity.SysVeterans;
import org.jeecg.modules.veterans.entity.SysVeteransCard;
import org.jeecg.modules.veterans.mapper.SysVeteransCardMapper;
import org.jeecg.modules.veterans.mapper.SysVeteransMapper;
import org.jeecg.modules.veterans.service.ISysVeteransService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 退役军人信息表
 * @Author: jeecg-boot
 * @Date:   2022-03-27
 * @Version: V1.0
 */
@Service
public class SysVeteransServiceImpl extends ServiceImpl<SysVeteransMapper, SysVeterans> implements ISysVeteransService {

	@Autowired
	private SysVeteransMapper sysVeteransMapper;
	@Autowired
	private SysVeteransCardMapper sysVeteransCardMapper;
	
	@Override
	@Transactional
	public void saveMain(SysVeterans sysVeterans, List<SysVeteransCard> sysVeteransCardList) {
		sysVeteransMapper.insert(sysVeterans);
		if(sysVeteransCardList!=null && sysVeteransCardList.size()>0) {
			for(SysVeteransCard entity:sysVeteransCardList) {
				//外键设置
				entity.setVeteransId(sysVeterans.getId());
				sysVeteransCardMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(SysVeterans sysVeterans,List<SysVeteransCard> sysVeteransCardList) {
		sysVeteransMapper.updateById(sysVeterans);
		
		//1.先删除子表数据
		sysVeteransCardMapper.deleteByMainId(sysVeterans.getId());
		
		//2.子表数据重新插入
		if(sysVeteransCardList!=null && sysVeteransCardList.size()>0) {
			for(SysVeteransCard entity:sysVeteransCardList) {
				//外键设置
				entity.setVeteransId(sysVeterans.getId());
				sysVeteransCardMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		sysVeteransCardMapper.deleteByMainId(id);
		sysVeteransMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			sysVeteransCardMapper.deleteByMainId(id.toString());
			sysVeteransMapper.deleteById(id);
		}
	}

	@Override
	public SysVeterans selectVeteransByPhone(String phone){
		return sysVeteransMapper.selectVeteransByPhone(phone);
	}

	@Override
	public SysVeterans selectVeteransBySfzh(String sfzh){
		return sysVeteransMapper.selectVeteransBySfzh(sfzh);
	}

	@Override
	public SysVeteranSta veteranSta() {
		return sysVeteransMapper.veteranSta();
	}

	@Override
	public List<SysNameValueVo> veteranAgeSta() {
		return baseMapper.veteranAgeSta();
	}
}
